﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Data;
using Tools;
using Wp.DAL;

namespace Wp.BLL
{
    /// <summary>
    /// Summary description for TblPosts
    /// </summary>
    public class TblPostsManagerBase
    {
        public TblPostsManagerBase()
        {
            //
            // TODO: Add constructor logic here
            //
        }
        public int InsertUpdate(TblPostsEntity item)
        {
            try
            {
                List<SqlParameter> ls = new List<SqlParameter>();
                SqlParameter sp = new SqlParameter("@Output", item.Id);
                sp.Direction = System.Data.ParameterDirection.Output;
                ls.Add(sp);
                ls.Add(new SqlParameter("@Id", item.Id));
                ls.Add(new SqlParameter("@TextId", item.TextId));
                ls.Add(new SqlParameter("@Title", item.Title));
                ls.Add(new SqlParameter("@PostType", item.PostType));
                ls.Add(new SqlParameter("@Summary", item.Summary));
                ls.Add(new SqlParameter("@Description", item.Description));
                ls.Add(new SqlParameter("@ImageUrl", item.ImageUrl));
                ls.Add(new SqlParameter("@Author", item.Author));
                ls.Add(new SqlParameter("@Url", item.Url));
                ls.Add(new SqlParameter("@CreatedBy", item.CreatedBy));
                ls.Add(new SqlParameter("@CreatedDate", item.CreatedDate));
                ls.Add(new SqlParameter("@ModifiedBy", item.ModifiedBy));
                ls.Add(new SqlParameter("@ModifiedDate", item.ModifiedDate));
                ls.Add(new SqlParameter("@DisplayOrder", item.DisplayOrder));
                ls.Add(new SqlParameter("@ParentId", item.ParentId));
                ls.Add(new SqlParameter("@PostStatus", item.PostStatus));
                ls.Add(new SqlParameter("@CommentStatus", item.CommentStatus));
                ls.Add(new SqlParameter("@PublishDate", item.PublishDate));
                SqlParameter[] sqlparam = ls.ToArray();
                SqlHelper.ExecuteNonQuery(SqlHelper.WpConnectString, CommandType.StoredProcedure, "wp_InsertUpdateTblPost", sqlparam);
                return int.Parse(sqlparam[0].Value.ToString());
            }
            catch (Exception)
            {
                return -1;
            }
        }
        public int InsertUpdate(int Id, string TextId, string Title, string PostType, string Summary, string Description, string ImageUrl, string Author, string Url, string CreatedBy, System.DateTime CreatedDate, string ModifiedBy, System.DateTime ModifiedDate, int DisplayOrder, int ParentId, string PostStatus, string CommentStatus, System.DateTime PublishDate)
        {
            List<SqlParameter> ls = new List<SqlParameter>();
            SqlParameter sp = new SqlParameter("@Output", Id);
            sp.Direction = System.Data.ParameterDirection.Output;
            ls.Add(sp);
            ls.Add(new SqlParameter("@Id", Id));
            ls.Add(new SqlParameter("@TextId", TextId));
            ls.Add(new SqlParameter("@Title", Title));
            ls.Add(new SqlParameter("@PostType", PostType));
            ls.Add(new SqlParameter("@Summary", Summary));
            ls.Add(new SqlParameter("@Description", Description));
            ls.Add(new SqlParameter("@ImageUrl", ImageUrl));
            ls.Add(new SqlParameter("@Author", Author));
            ls.Add(new SqlParameter("@Url", Url));
            ls.Add(new SqlParameter("@CreatedBy", CreatedBy));
            ls.Add(new SqlParameter("@CreatedDate", CreatedDate));
            ls.Add(new SqlParameter("@ModifiedBy", ModifiedBy));
            ls.Add(new SqlParameter("@ModifiedDate", ModifiedDate));
            ls.Add(new SqlParameter("@DisplayOrder", DisplayOrder));
            ls.Add(new SqlParameter("@ParentId", ParentId));
            ls.Add(new SqlParameter("@PostStatus", PostStatus));
            ls.Add(new SqlParameter("@CommentStatus", CommentStatus));
            ls.Add(new SqlParameter("@PublishDate", PublishDate));
            SqlParameter[] sqlparam = ls.ToArray();
            SqlHelper.ExecuteNonQuery(SqlHelper.WpConnectString, CommandType.StoredProcedure, "wp_InsertUpdateTblPost", sqlparam);
            return int.Parse(sqlparam[0].Value.ToString());
        }
        public string GetUniqueTextIdFromUnicodeText(string UnicodeText)
        {
            string strSql = @"  SELECT TextId FROM TblPosts tn
                                WHERE tn.TextId=@TextId";
            string textId = WebUtility.GetUniqueTextIdFromUnicodeText(UnicodeText, "@TextId", strSql
                , SqlHelper.WpConnectString, 255);
            return textId;
        }
        public void Delete(int Id)
        {
            List<SqlParameter> ls = new List<SqlParameter>();
            ls.Add(new SqlParameter("@Id", Id));
            SqlParameter[] sqlparam = ls.ToArray();
            SqlHelper.ExecuteNonQuery(SqlHelper.WpConnectString, CommandType.StoredProcedure, "wp_DeleteTblPost", sqlparam);
        }
        public TblPostsEntity GetTblPostsEntity(DataRow dr)
        {
            TblPostsEntity item = new TblPostsEntity();
            if (dr.Table.Columns.Contains("Id"))
                item.Id = dr["Id"].ToInt();
            if (dr.Table.Columns.Contains("TextId"))
                item.TextId = dr["TextId"].ToString();
            if (dr.Table.Columns.Contains("Title"))
                item.Title = dr["Title"].ToString();
            if (dr.Table.Columns.Contains("PostType"))
                item.PostType = dr["PostType"].ToString();
            if (dr.Table.Columns.Contains("Summary"))
                item.Summary = dr["Summary"].ToString();
            if (dr.Table.Columns.Contains("Description"))
                item.Description = dr["Description"].ToString();
            if (dr.Table.Columns.Contains("ImageUrl"))
                item.ImageUrl = dr["ImageUrl"].ToString();
            if (dr.Table.Columns.Contains("Author"))
                item.Author = dr["Author"].ToString();
            if (dr.Table.Columns.Contains("Url"))
                item.Url = dr["Url"].ToString();
            if (dr.Table.Columns.Contains("CreatedBy"))
                item.CreatedBy = dr["CreatedBy"].ToString();
            if (dr.Table.Columns.Contains("CreatedDate"))
                item.CreatedDate = dr["CreatedDate"].ToDateTime();
            if (dr.Table.Columns.Contains("ModifiedBy"))
                item.ModifiedBy = dr["ModifiedBy"].ToString();
            if (dr.Table.Columns.Contains("ModifiedDate"))
                item.ModifiedDate = dr["ModifiedDate"].ToDateTime();
            if (dr.Table.Columns.Contains("DisplayOrder"))
                item.DisplayOrder = dr["DisplayOrder"].ToInt();
            if (dr.Table.Columns.Contains("ParentId"))
                item.ParentId = dr["ParentId"].ToInt();
            if (dr.Table.Columns.Contains("PostStatus"))
                item.PostStatus = dr["PostStatus"].ToString();
            if (dr.Table.Columns.Contains("CommentStatus"))
                item.CommentStatus = dr["CommentStatus"].ToString();
            if (dr.Table.Columns.Contains("PublishDate"))
                item.PublishDate = dr["PublishDate"].ToDateTime();
            if (dr.Table.Columns.Contains("TermTaxonomyId"))
                item.TermTaxonomyId = dr["TermTaxonomyId"].ToInt();
            if (dr.Table.Columns.Contains("TermTaxonomyName"))
                item.TermTaxonomyName = dr["TermTaxonomyName"].ToString();
            if (dr.Table.Columns.Contains("Slug"))
                item.Slug = dr["Slug"].ToString();
            if (dr.Table.Columns.Contains("NumView"))
                item.NumView = dr["NumView"].ToInt();
            return item;
        }
        //public TblPostsEntity GetTblPostsEntity(DataRow dr, bool allchild)
        //{
        //    TblPostsEntity item = new TblPostsEntity();
        //    if (dr.Table.Columns.Contains("Id"))
        //        item.Id = dr["Id"].ToInt();
        //    if (dr.Table.Columns.Contains("TextId"))
        //        item.TextId = dr["TextId"].ToString();
        //    if (dr.Table.Columns.Contains("Title"))
        //        item.Title = dr["Title"].ToString();
        //    if (dr.Table.Columns.Contains("PostType"))
        //        item.PostType = dr["PostType"].ToString();
        //    if (dr.Table.Columns.Contains("Summary"))
        //        item.Summary = dr["Summary"].ToString();
        //    if (dr.Table.Columns.Contains("Description"))
        //        item.Description = dr["Description"].ToString();
        //    if (dr.Table.Columns.Contains("ImageUrl"))
        //        item.ImageUrl = dr["ImageUrl"].ToString();
        //    if (dr.Table.Columns.Contains("Author"))
        //        item.Author = dr["Author"].ToString();
        //    if (dr.Table.Columns.Contains("Url"))
        //        item.Url = dr["Url"].ToString();
        //    if (dr.Table.Columns.Contains("CreatedBy"))
        //        item.CreatedBy = dr["CreatedBy"].ToString();
        //    if (dr.Table.Columns.Contains("CreatedDate"))
        //        item.CreatedDate = dr["CreatedDate"].ToDateTime();
        //    if (dr.Table.Columns.Contains("ModifiedBy"))
        //        item.ModifiedBy = dr["ModifiedBy"].ToString();
        //    if (dr.Table.Columns.Contains("ModifiedDate"))
        //        item.ModifiedDate = dr["ModifiedDate"].ToDateTime();
        //    if (dr.Table.Columns.Contains("DisplayOrder"))
        //        item.DisplayOrder = dr["DisplayOrder"].ToInt();
        //    if (dr.Table.Columns.Contains("ParentId"))
        //        item.ParentId = dr["ParentId"].ToInt();
        //    if (dr.Table.Columns.Contains("PostStatus"))
        //        item.PostStatus = dr["PostStatus"].ToString();
        //    if (dr.Table.Columns.Contains("CommentStatus"))
        //        item.CommentStatus = dr["CommentStatus"].ToString();
        //    if (dr.Table.Columns.Contains("PublishDate"))
        //        item.PublishDate = dr["PublishDate"].ToDateTime();
        //    if (dr.Table.Columns.Contains("TermTaxonomyId"))
        //        item.TermTaxonomyId = dr["TermTaxonomyId"].ToInt();
        //    if (dr.Table.Columns.Contains("TermTaxonomyName"))
        //        item.TermTaxonomyName = dr["TermTaxonomyName"].ToString();
        //    if (dr.Table.Columns.Contains("Slug"))
        //        item.Slug = dr["Slug"].ToString();
        //    List<TblPostMetaEntity> ls = TblPostMetaManager.CreateInstant().SelectByPostId(item.Id);
        //    if (ls != null)
        //    {
        //        for (int i = 0; i < ls.Count; i++)
        //        {
        //            item.PostMetas.Add(ls[i].MetaKey, ls[i].MetaValue);
        //        }
        //    }
        //    return item;
        //}
        public DataTable SelectOneRDT(int Id)
        {
            List<SqlParameter> ls = new List<SqlParameter>();
            ls.Add(new SqlParameter("@Id", Id));
            SqlParameter[] sqlparam = ls.ToArray();
            return SqlHelper.ExecuteDataTable(SqlHelper.WpConnectString, CommandType.StoredProcedure, "wp_SelectTblPost", sqlparam);
        }
        public TblPostsEntity SelectOne(int Id)
        {
            List<SqlParameter> ls = new List<SqlParameter>();
            ls.Add(new SqlParameter("@Id", Id));
            SqlParameter[] sqlparam = ls.ToArray();
            DataTable dt = SqlHelper.ExecuteDataTable(SqlHelper.WpConnectString, CommandType.StoredProcedure, "wp_SelectTblPost", sqlparam);
            if (dt != null && dt.Rows.Count > 0)
                return GetTblPostsEntity(dt.Rows[0]);
            return null;
        }
       
        public DataTable SelectAllRDT()
        {
            return SqlHelper.ExecuteDataTable(SqlHelper.WpConnectString, CommandType.StoredProcedure, "wp_SelectTblPostsAll", null);
        }
        public List<TblPostsEntity> SelectAll()
        {
            List<TblPostsEntity> lsresult = new List<TblPostsEntity>();
            DataTable dt = SqlHelper.ExecuteDataTable(SqlHelper.WpConnectString, CommandType.StoredProcedure, "wp_SelectTblPostsAll", null);
            if (dt != null && dt.Rows.Count > 0)
                for (int i = 0; i < dt.Rows.Count; i++)
                    lsresult.Add(GetTblPostsEntity(dt.Rows[i]));
            return lsresult;
        }
    }
}

